package com.ideabove.util.kakao;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;

import com.ideabove.radio.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;

public class KakaoUtil {
	Activity mActivity;
	private String encoding = "UTF-8";
	
	public KakaoUtil(Activity activity){
		this.mActivity = activity;
	}
	/**
	 * Send URL
	 * @throws NameNotFoundException 
	 */
	public void sendUrlLink(View v) throws NameNotFoundException {
		// Recommended: Use application context for parameter.
		KakaoLink kakaoLink = KakaoLink.getLink(mActivity.getApplicationContext());

		// check, intent is available.
		if (!kakaoLink.isAvailableIntent()) {
			alert("Not installed KakaoTalk.");			
			return;
		}

		/**
		 * @param activity
		 * @param url
		 * @param message
		 * @param appId
		 * @param appVer
		 * @param appName
		 * @param encoding
		 */
		kakaoLink.openKakaoLink(mActivity, 
				"http://link.kakao.com/?test-android-app", 
				"First KakaoLink Message for send url.", 
				mActivity.getPackageName(), 
				mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), 0).versionName, 
				"제목", 
				encoding);
	}

	/**
	 * Send App data
	 */
	public void sendAppData(String message, String webParams, String execParams) throws NameNotFoundException {
		ArrayList<Map<String, String>> metaInfoArray = new ArrayList<Map<String, String>>();

		// If application is support Android platform.
		Map<String, String> metaInfoAndroid = new Hashtable<String, String>(1);
		metaInfoAndroid.put("os", "android");
		metaInfoAndroid.put("devicetype", "phone");
		metaInfoAndroid.put("installurl", "http://gift.bonosound.com/?" + webParams);
		metaInfoAndroid.put("executeurl", "ideabove://com.ideabove.radio?q=playone" + execParams);
		
		
		
		
		
		// If application is support ios platform.
		Map<String, String> metaInfoIOS = new Hashtable<String, String>(1);
		metaInfoIOS.put("os", "ios");
		metaInfoIOS.put("devicetype", "phone");
		metaInfoIOS.put("installurl", "http://gift.bonosound.com/?" + webParams);
		metaInfoIOS.put("executeurl", "ideabove://com.ideabove.radio?q=playone" + execParams);
		
		// add to array
		metaInfoArray.add(metaInfoAndroid);
		metaInfoArray.add(metaInfoIOS);
		
		// Recommended: Use application context for parameter. 
		KakaoLink kakaoLink = KakaoLink.getLink(mActivity.getApplicationContext());
		
		// check, intent is available.
		if(!kakaoLink.isAvailableIntent()) {
			alert("Not installed KakaoTalk.");			
			return;
		}
		
		/**
		 * @param activity
		 * @param url
		 * @param message
		 * @param appId
		 * @param appVer
		 * @param appName
		 * @param encoding
		 * @param metaInfoArray
		 */
		kakaoLink.openKakaoAppLink(
				mActivity, 
				"http://link.kakao.com/?test-android-app", 
				message,  
				mActivity.getPackageName(), 
				mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), 0).versionName,
				"음악 선물이 도착했습니다",
				encoding, 
				metaInfoArray);
	}

	
	/**
	 * Story Link
	 * Send posting (text or url)
	 * @throws NameNotFoundException 
	 */
	public void sendPostingLink(View v) throws NameNotFoundException {
		Map<String, Object> urlInfoAndroid = new Hashtable<String, Object>(1);
		urlInfoAndroid.put("title", "(광해) 실제 역사적 진실은?");
		urlInfoAndroid.put("desc", "(광해 왕이 된 남자)의 역사성 부족을 논하다.");
		urlInfoAndroid.put("imageurl", new String[] {"http://m1.daumcdn.net/photo-media/201209/27/ohmynews/R_430x0_20120927141307222.jpg"});
		urlInfoAndroid.put("type", "article");
		
		// Recommended: Use application context for parameter.
		StoryLink storyLink = StoryLink.getLink(mActivity.getApplicationContext());

		// check, intent is available.
		if (!storyLink.isAvailableIntent()) {
			alert("Not installed KakaoStory.");			
			return;
		}

		/**
		 * @param activity
		 * @param post (message or url)
		 * @param appId
		 * @param appVer
		 * @param appName
		 * @param encoding
		 * @param urlInfoArray
		 */
		storyLink.openKakaoLink(mActivity, 
				"http://m.media.daum.net/entertain/enews/view?newsid=20120927110708426",
				mActivity.getPackageName(), 
				mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), 0).versionName, 
				"미디어디음",
				encoding, 
				urlInfoAndroid);
	}
	
	/**
	 * Story Link
	 * Send posting (image)
	 * @throws NameNotFoundException 
	 */
	public void sendPostingImage(View v) throws NameNotFoundException {
		StoryLink storyLink = StoryLink.getLink(mActivity.getApplicationContext());
		
		// check, intent is available.
		if (!storyLink.isAvailableIntent()) {
			alert("Not installed KakaoStory.");
			return;
		}
		
		Intent intent = new Intent(Intent.ACTION_PICK);
		intent.setType("image/*");
		intent.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		mActivity.startActivityForResult(intent, 0); 
	}
	
	private void alert(String message) {
		new AlertDialog.Builder(mActivity)
			.setIcon(android.R.drawable.ic_dialog_alert)
			.setTitle(R.string.app_name)
			.setMessage(message)
			.setPositiveButton(android.R.string.ok, null)
			.create().show();
	}
}
